﻿using GammaJul.ReSharper.GammaXaml.Highlightings.Xaml;
using JetBrains.Application;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Daemon;
using JetBrains.ReSharper.Feature.Services.Navigation;
using JetBrains.ReSharper.Psi;
using JetBrains.TextControl.Markup;
using JetBrains.UI.Icons;

namespace GammaJul.ReSharper.GammaXaml.Caliburn {

	public abstract class CaliburnGutterMark : IconGutterMark {

		public override bool IsClickable {
			get { return true; }
		}

		public override void OnClick(IHighlighter highlighter) {
			ISolution solution = Shell.Instance.GetComponent<SolutionsManager>().Solution;
			if (solution == null)
				return;

			var mark = Daemon.GetInstance(solution).GetHighlighting(highlighter) as CaliburnBoundMarkOnGutter;
			if (mark == null)
				return;

			IDeclaredElement declaredElement = mark.Reference.Resolve().DeclaredElement;
			if (declaredElement == null)
				return;

			using (ReadLockCookie.Create())
				NavigationManager.NavigateToDeclaredElement(declaredElement, true);
		}

		protected CaliburnGutterMark(IconId iconId, IThemedIconManager themedIconManager)
			: base(iconId, themedIconManager) {
		}

	}

}